﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
///YqhData 的摘要说明
/// </summary>
public class YqhData
{
    /// <summary>
    /// 数据库链接字符串
    /// 需要在配置文件中提供如下节点内容
    /// <connectionStrings>
    ///		<add name="SqlConn" connectionString="Server=数据库服务器名称或地址;Database=master;User ID=sa;pwd=;"/>
    /// </connectionStrings>
    /// </summary>
    private static string _ConnectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();

    protected static SqlConnection conn;

    protected static SqlCommand comm = new SqlCommand();
	public YqhData()
	{
        openConnection();
	}

    #region 数据访问

    /// <summary>
    /// 打开数据库连接
    /// </summary>
    private void openConnection()
    {
        try
        {
            if (conn == null)
            {
                conn = new SqlConnection(_ConnectionString);
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            comm.Connection = conn;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }

    /// <summary>
    /// 关闭当前数据库连接
    /// </summary>
    private void closeConnection()
    {
        try
        {
            if (conn != null && conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
            if (conn != null)
            {
                conn.Dispose();
            }
            comm.Dispose();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }

    /// <summary>
    /// 释放系统资源
    /// </summary>
    public void Dispose()
    {
        closeConnection();
    }

    #endregion

    #region 数据操作
    public SqlCommand GetCmd(string sqlstr)
    {
        SqlCommand objCmd = new SqlCommand();
        objCmd.Connection = conn;
        objCmd.CommandType = CommandType.Text;
        objCmd.CommandText = sqlstr;
        return objCmd;
    }

    public int ExecuteSql(string sqlstr)
    {
        int i = GetCmd(sqlstr).ExecuteNonQuery();
        return i;
    }

    public object ExecuteScalar(string sqlstr)
    {
        object i = GetCmd(sqlstr).ExecuteScalar();
        return i;
    }

    public SqlDataReader ExecuteDataReader(string sqlstr)
    {
        SqlDataReader dr = null;
        try
        {
            comm.CommandText = sqlstr;
            comm.CommandType = CommandType.Text;
            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch
        {
            dr.Close();
        }
        return dr;
    }   
    
    public DataSet FillDataSet(string selSQL)
    {
        SqlDataAdapter objda = new SqlDataAdapter();
        objda.SelectCommand = GetCmd(selSQL);
        DataSet objds = new DataSet();
        objda.Fill(objds);
        return objds;
    }

    public DataSet ExecuteDataSet(string sqlstr, int pageIndex, int pageSize)
    {
        //设置导入的起始地址
        int firstPage = pageIndex * pageSize;

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();
        try
        {
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds, firstPage, pageSize, "PranaTable");
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        return ds;
    }
    #endregion


    public void ShowMsg(string info)
    {
        System.Web.HttpContext.Current.Response.Write(string.Format("<script language='javascript'>alert('{0}')</script>", info));
    }
    public void ShowMsg(string info, string url)
    {
        System.Web.HttpContext.Current.Response.Write(string.Format("<script>alert('{0}');window.location.href = '{1}';</script>", info,url));
    }

    public string Msg(string info)
    {
       string msg = string.Format("<script language='javascript'>alert('{0}')</script>", info);
       return msg;
    }
    public string Msg(string info, string url)
    {
        string msg = string.Format("<script>alert('{0}');window.location.href = '{1}';</script>");
        return msg;
    }
}